package com.itheima;

import com.itheima.pojo.User;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wmh
 * @version 1.0 2022/5/25
 * @Description: 测试导入数据
 **/
public class POIDemo3 {
    //用户名，主机号，省份，城市，工资，入职日期，出生日期，现住地址
    public static void main(String[] args) throws Exception {

        SimpleDateFormat simpleDateFormat = new  SimpleDateFormat("yyyy-MM-dd");

        //有内容的workbook
        Workbook workbook = new XSSFWorkbook(new FileInputStream("D:/用户导入测试数据.xlsx"));
        //获取到第一个工作表
        Sheet sheet = workbook.getSheetAt(0);
        //读取工作表中的内容
        //获取当前sheet的最后一行的索引值
        int lastRowIndex = sheet.getLastRowNum();
        Row row = null;
        User user = new User();
        //读取工作表中的内容
        for (int i = 1; i <= lastRowIndex; i++) {
              row = sheet.getRow(i);
            String username = row.getCell(0).getStringCellValue();
            String phone = null;
            try {
                phone = row.getCell(1).getStringCellValue()+"";
            } catch (Exception e) {
                e.printStackTrace();
            }
            String province = row.getCell(2).getStringCellValue();
            String city = row.getCell(3).getStringCellValue();
            Integer salary = ((Double) row.getCell(4).getNumericCellValue()).intValue();
            Date hireDate = simpleDateFormat.parse(row.getCell(5).getStringCellValue());
            Date birthDay = simpleDateFormat.parse(row.getCell(6).getStringCellValue());
            String address = row.getCell(7).getStringCellValue();

            user.setUserName(username);
            user.setBirthday(birthDay);
            user.setAddress(address);
            user.setCity(city);
            user.setHireDate(hireDate);
            user.setProvince(province);
            user.setSalary(salary);
            user.setPhone(phone);
            //执行插入方法
            System.out.println(user.toString());
        }
    }
}